﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpSetupWorkPlaneDlg.h "
#include "UiCommonDialog.h"
#include "GmStyleSheetHelper.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

namespace gcmp{

    GbmpSetupWorkPlaneDlg::GbmpSetupWorkPlaneDlg(RunParam& rp, QWidget *parent)
        :QDialog(parent, Qt::FramelessWindowHint)
    {
        m_pRunParam = &rp;
        m_ui.setupUi(this);
        std::wstring styleSheetStr = GmStyleSheetHelper::Instance()->GetCurrentStyleData();
        setStyleSheet(QString::fromStdWString(styleSheetStr));
        m_ui.btnShowWorkPlane->setEnabled(false);
        m_ui.btnCancelReference->setEnabled(false);
        m_ui.rbWorkPlaneNames->setChecked(true);
        m_ui.rbPickCurve->setVisible(false);
        m_ui.gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        m_ui.gbmp_title->setIconVisible(false);
        m_ui.gbmp_title->setMaxButtonVisible(false);
        m_ui.gbmp_title->setMinButtonVisible(false);
        m_ui.gbmp_title->setText(QString::fromStdWString(GBMP_TR(L"工作平面")));
        this->setProperty("use_gbmp_title", true);
        m_ui.gbCurrentWorkPlane->setVisible(false);
        //if (gcmp::ProductConfig::GetProductConfig()->GetProductType() == gcmp::PT_FamilyEditor)
        //{
        //    m_ui.rbPickCurve->setVisible(false);
        //}
        QStringList workPlaneNames;
        for each(auto&s in rp.m_arrViewName)
        {
            workPlaneNames.append(QString::fromStdWString(s));
        }
        m_ui.cbWorkPlaneNames->addItems(workPlaneNames);

        m_radioIndex = 0;
        m_ui.cbWorkPlaneNames->setCurrentIndex(rp.m_idxCurView);
        if (rp.m_idxCurView>=0)
            m_ui.labelWorkPlaneName->setText(QString::fromStdWString(rp.m_arrViewName[rp.m_idxCurView]));
        QObject::connect(m_ui.rbPickPlane, SIGNAL(clicked()), this, SLOT(onRbPickPlaneClicked()));
        QObject::connect(m_ui.rbWorkPlaneNames, SIGNAL(clicked()), this, SLOT(onRbWorkPlaneNamesClicked()));

        connect(m_ui.gbmp_title, &GmTitleBar::closeRequested, this, &GbmpSetupWorkPlaneDlg::reject);
        connect(m_ui.gbmp_title, &GmTitleBar::moveRequested, this, &GbmpSetupWorkPlaneDlg::onMoveRequested);
    }

    void GbmpSetupWorkPlaneDlg::accept()
    {
        m_workPlaneIndex = m_ui.cbWorkPlaneNames->currentIndex();
        if (m_ui.rbWorkPlaneNames->isChecked())
        {
            if (m_workPlaneIndex < 0)
            {
                gcmp::UiCommonDialog::ShowMessageBox(GBMP_TR(L"工作平面设置"), GBMP_TR(L"请选择参考平面！"), (int)gcmp::UiCommonDialog::ButtonType::OK);
                return;
            }
            m_radioIndex = 0;
            m_pRunParam->m_rt = RunParam::RT_BY_NAME;
            //gux::GuxClientUtil::LogCustomData(m_ui.rbWorkPlaneNames->text().toStdWString(), m_ui.cbWorkPlaneNames->currentText().toStdWString());
        }
        else if (m_ui.rbPickPlane->isChecked())
        {
            m_pRunParam->m_rt = RunParam::RT_PICKFACE;
            m_radioIndex = 1;
            //gux::GuxClientUtil::LogCustomData(m_ui.rbPickPlane->text().toStdWString(), L"Yes");
        }
        else if (m_ui.rbPickCurve->isChecked())
        {
            m_pRunParam->m_rt = RunParam::RT_PICKLINE;
            m_radioIndex = 2;
            //gux::GuxClientUtil::LogCustomData(m_ui.rbPickCurve->text().toStdWString(), L"Yes");
        }

        m_pRunParam->m_idxCurView = m_workPlaneIndex;

        QDialog::accept();
    }

    void GbmpSetupWorkPlaneDlg::reject()
    {
        m_pRunParam->m_rt = RunParam::RT_CANCEL;
        QDialog::reject();
    }

    void GbmpSetupWorkPlaneDlg::currentIndexChanged(int index)
    {

    }

    void GbmpSetupWorkPlaneDlg::onRbWorkPlaneNamesClicked()
    {
        m_ui.cbWorkPlaneNames->setEnabled(true);
    }

    void GbmpSetupWorkPlaneDlg::onRbPickPlaneClicked()
    {
        m_ui.cbWorkPlaneNames->setEnabled(false);
    }

    void GbmpSetupWorkPlaneDlg::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
    {
        Q_UNUSED(cursorPos);
        move(pos() + offset);
    }


}


